package cn.tjnext.apply.service;

import cn.tjnext.apply.contants.EntityStatus;
import cn.tjnext.apply.dao.AdminDao;
import cn.tjnext.apply.entity.AdminEntity;
import cn.tjnext.apply.repositories.AdminRepository;
import cn.tjnext.apply.utils.EncryptHelper;
import cn.tjnext.apply.web.model.CheckLoginModel;
import cn.tjnext.apply.web.model.LoginModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by cp on 2017/7/31.
 */
@Service
@Transactional(readOnly = true)
public class AdminService extends AbstractService<AdminEntity, AdminRepository, AdminDao> {
    protected static final Logger log = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private AdminDao adminDao;

    @Override
    protected AdminDao getDao() {
        return adminDao;
    }

    /**
     * 验证登录密码
     * @param account
     * @param password
     * @return
     */
    public CheckLoginModel checkPassword(String account, String password) {
        CheckLoginModel checkLoginModel = new CheckLoginModel();
        AdminEntity adminEntity = adminDao.findByAccount(account);
        checkLoginModel.setUserId(adminEntity.getPkId());
        if (adminEntity !=null) {
            String md5Password = null;
            try {
                md5Password = EncryptHelper.encodeMD5(password);
            } catch (Exception e) {
                return new CheckLoginModel(e.getMessage());
            }
            if (!adminEntity.getPassword().equals(md5Password)) {
                return new CheckLoginModel("密码错误");
            };
        }
        return checkLoginModel;
    }

    /**
     * 验证登录名密码
     * @param loginModel
     * @return
     */
    public LoginModel login(LoginModel loginModel) {
        try {
            if (loginModel != null) {
                // 验证空
                if (StringUtils.isEmpty(loginModel.getLoginName())) {
                    loginModel.setErrorMessage("请输入用户名");
                    return loginModel;
                }
                if (StringUtils.isEmpty(loginModel.getPassword())) {
                    loginModel.setErrorMessage("请输入密码");
                    return loginModel;
                }

                // 验证用户名和密码
                AdminEntity adminEntity = adminDao.findByAccount(loginModel.getLoginName());
                if (adminEntity == null) {
                    loginModel.setErrorMessage("用户名不存在");
                    return loginModel;
                }
                try {
                    loginModel.setPassword(EncryptHelper.encodeMD5(loginModel.getPassword()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if ( ! loginModel.getPassword().equals(adminEntity.getPassword())) {
                    loginModel.setErrorMessage("密码错误");
                    return loginModel;
                }
                if (Objects.equals(adminEntity.getStatus(), EntityStatus.DISABLED)) {
                    loginModel.setErrorMessage("用户已被禁用");
                    return loginModel;
                }
                adminEntity.setLastTime(new Date());
                loginModel.setAdminEntity(adminEntity);
            } else {
                loginModel = new LoginModel();
                loginModel.setErrorMessage("未知错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            assert loginModel != null;
            log.error("登陆失败：" + loginModel.getLoginName() + ", " + loginModel.getPassword(), e);
        }
        return loginModel;
    }
}
